﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace WindowsFormsApplication1.EnterGameRequests
{
    public class EnterGameRequest
    {
        public ClientSocketData clientSocketData;
        public ServerSocketData transferToServer;

        public int characterID;
        public int MapID;
        public int Instance;
        public int MapX;
        public int MapY;
        public string uid;

        private Timer requestTimeout = new Timer(10000);

        public EnterGameRequest(ClientSocketData csd)
        {
            clientSocketData = csd;
            requestTimeout.AutoReset = false;
            requestTimeout.Elapsed += new ElapsedEventHandler(requestTimeout_Elapsed);
            requestTimeout.Start();
        }

        private void requestTimeout_Elapsed(object sender, ElapsedEventArgs e)
        {
            Managers.enterGameManager.ProcessTimeout(this);
        }
        private void killTimeout()
        {
            try
            {
                requestTimeout.Stop();
                requestTimeout.Dispose();
            }
            catch { }
        }
        public void processTimeout()
        {
            killTimeout();
            try
            {
                string xmlMessage = "<entergameresult error=\"World Server did not respond. Please email this error to Webmaster@RpgReborn.com\" />";
                clientSocketData.socket.BeginSend(xmlMessage);
            }
            catch { }
        }

        public void processResult(bool result)
        {
            killTimeout();
            try
            {
                if (result == true)
                {
                    string xmlMessage = "<entergameresult port=\"" + transferToServer.clientConnectPort.ToString() + "\" uid=\"" + uid + "\" />";
                    clientSocketData.socket.BeginSend(xmlMessage);
                }
                else
                {
                    string xmlMessage = "<entergameresult error=\"Server already has pending enter game request\" />";
                    clientSocketData.socket.BeginSend(xmlMessage);
                }
            }
            catch { }
        }
    }
}
